//
//  SunMyCardsVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/26.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunMyCardsVC.h"
#import "SunCardsCell.h"
#import "SunWebVC.h"

@interface SunMyCardsVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIView *_blank;
    UITableView *_tb;
    UIView *_scroll;
    NSArray *_dataArr;
    UIButton *_go;
}
@property (nonatomic, strong) UIButton *onButton;
@property (nonatomic, strong) UIButton *useButton;
@property (nonatomic, strong) UIButton *dateButton;
@property (nonatomic, strong) UILabel *redLine;
@property (nonatomic, strong) NSString *type;

@end

@implementation SunMyCardsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的卡券";
//    self.view.backgroundColor = BLACK;
    [self creatUI];
    self.type = @"1";
    [self requestToCards];
    // Do any additional setup after loading the view.
}


- (void)creatUI{
    UIButton *help = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, X(25), Y(25))];
    [help setImage:[UIImage imageNamed:@"third_help"] forState:UIControlStateNormal];
    [help addTarget:self action:@selector(help) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:help];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    self.onButton = [[UIButton alloc]init];
    [self.view addSubview:self.onButton];
    [self.onButton setFrame:CGRectMake(0, 0, SCREENWIDTH/3-1, 45)];
    [self.onButton setTitle:@"未使用" forState:UIControlStateNormal];
    [self.onButton setTitleColor:[UIColor colorWithHexString:@"B9B9B9"] forState:UIControlStateNormal];
    [self.onButton setTitleColor:[UIColor colorWithHexString:@"D0021B"] forState:UIControlStateSelected];
    self.onButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13];
    [self.onButton addTarget:self action:@selector(onAction) forControlEvents:UIControlEventTouchUpInside];
    self.onButton.selected = YES;
    
    self.redLine = [[UILabel alloc]init];
    [self.view addSubview:self.redLine];
    self.redLine.backgroundColor = [UIColor colorWithHexString:@"D0021B"];
    [self.redLine setFrame:CGRectMake((SCREENWIDTH/3-X(45))/2, 43, X(45), 2)];

    
    UIImageView *line1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invest_line"]];
    [self.view addSubview:line1];
    [line1 setFrame:CGRectMake(SCREENWIDTH/3-1, 18, 1, 20)];
    
    self.useButton = [[UIButton alloc]init];
    [self.view addSubview:self.useButton];
    [self.useButton setFrame:CGRectMake(SCREENWIDTH/3, 0, SCREENWIDTH/3-1, 45)];
    [self.useButton setTitle:@"已使用" forState:UIControlStateNormal];
    [self.useButton setTitleColor:[UIColor colorWithHexString:@"B9B9B9"] forState:UIControlStateNormal];
    [self.useButton setTitleColor:[UIColor colorWithHexString:@"D0021B"] forState:UIControlStateSelected];
    self.useButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13];
    [self.useButton addTarget:self action:@selector(useAction) forControlEvents:UIControlEventTouchUpInside];


    UIImageView *line2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invest_line"]];
    [self.view addSubview:line2];
    [line2 setFrame:CGRectMake(SCREENWIDTH/3*2-1, 18, 1, 20)];
    
    self.dateButton = [[UIButton alloc]init];
    [self.view addSubview:self.dateButton];
    [self.dateButton setFrame:CGRectMake(SCREENWIDTH/3*2, 0, SCREENWIDTH/3-1, 45)];
    [self.dateButton setTitle:@"已过期" forState:UIControlStateNormal];
    [self.dateButton setTitleColor:[UIColor colorWithHexString:@"B9B9B9"] forState:UIControlStateNormal];
    [self.dateButton setTitleColor:[UIColor colorWithHexString:@"D0021B"] forState:UIControlStateSelected];
    self.dateButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:13];
    [self.dateButton addTarget:self action:@selector(dateAction) forControlEvents:UIControlEventTouchUpInside];
    
    _tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStyleGrouped];
    [self.view addSubview:_tb];
    [_tb lt_All:SunEdgesMake(45, 0, 0, 0) save:YES];
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tb.showsVerticalScrollIndicator = NO;
}
#pragma mark- 未使用、已使用、已过期
- (void)onAction {
    self.onButton.selected = YES;
    self.useButton.selected = NO;
    self.dateButton.selected = NO;
    self.type = @"1";
    [self requestToCards];
    [self.redLine setFrame:CGRectMake((SCREENWIDTH/3-X(45))/2, 43, X(45), 2)];
}
- (void)useAction {
    self.onButton.selected = NO;
    self.useButton.selected = YES;
    self.dateButton.selected = NO;
    self.type = @"2";
    [self requestToCards];
    [self.redLine setFrame:CGRectMake(SCREENWIDTH/3+(SCREENWIDTH/3-X(45))/2, 43, X(45), 2)];

}
- (void)dateAction {
    self.onButton.selected = NO;
    self.useButton.selected = NO;
    self.dateButton.selected = YES;
    self.type = @"3";
    [self requestToCards];
    [self.redLine setFrame:CGRectMake(SCREENWIDTH/3*2+(SCREENWIDTH/3-X(45))/2, 43, X(45), 2)];
}
- (void)creatBlank{
    _blank = [[UIView alloc]init];
    [self.view addSubview:_blank];
    [_blank lt_All:SunEdgesMake(Y(46), 0, 0, 0) save:YES];
    _blank.backgroundColor = BACKGROUND;
    UIImageView *img = [[UIImageView alloc]init];
    [_blank addSubview:img];
    [img lt_XcenterTo:nil save:YES];
    img.image = [UIImage imageNamed:@"third_cards_blank"];
    UILabel *label = [[UILabel alloc]init];
    [_blank addSubview:label];
    [label lt_XcenterTo:nil save:YES];
    [label lt_setTop:Y(18) to:img save:YES];
    [label lt_layoutView:label side:SunCenterY to:nil toSide:SunCenterY withSize:1 offset:Y(20) save:YES];
    label.textColor = GRAY;
    label.font = [UIFont systemFontOfSize:19];
    label.text = @"暂无优惠券！";
}


- (void)help{
    SunWebVC *vc = [[SunWebVC alloc]init];
    vc.urlStr = [NSString stringWithFormat:@"%@/topic/card_des?source=ios",KWAP];
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark update
-(void)requestToCards{
    [[Sun shareSun]showActivity:self];
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_CARDS];
    NSDictionary *dic = @{@"type":self.type};
    [HttpRequest postURL:url withParam:dic success:^(id requestDic, NSString *msg) {
        if ([self.type isEqualToString:@"1"]) {
            _dataArr = [requestDic objectForKey:@"able"];
        }else if ([self.type isEqualToString:@"2"]){
            _dataArr = [requestDic objectForKey:@"isuse"];
        }else{
            _dataArr = [requestDic objectForKey:@"timeover"];
        }
        [[Sun shareSun]hideActivity];
        if (_dataArr.count == 0) {
            [self creatBlank];
        }else{
            [_tb reloadData];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

#pragma mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 125;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 15;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == _dataArr.count - 1) {
        return 15;
    }
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SunCardsCell *cell = [SunCardsCell cellWithTableView:tableView];
    [cell setCellData:_dataArr[indexPath.section] isUse:NO isAble:[self.type isEqualToString:@"1"] ? YES : NO];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
